package background

import (
	"individual/controllers"
	"individual/models"
	"math"
	"time"
)

type LeaveWordController struct {
	controllers.BaseController
}

// DeleteLeaveWordById
// @Title	DeleteLeaveWordById
// @Description 删除一级留言
// @Param id path	int	   true	"一级留言的id"
// @Success 201 删除一级留言留言成功
// @Failure 403 删除一级留言留言失败
// @router /delLeaveWordById/:id [delete]
func (c *LeaveWordController) DeleteLeaveWordById() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Delete(&models.LeaveWord{Id: id})
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "删除失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("删除成功")
	c.ServeJSON()
}

// DeleteLeaveWordTwoById
// @Title	DeleteLeaveWordTwoById
// @Description 删除二级留言
// @Param id path	int	   true	"二级留言的id"
// @Success 201 删除二级留言留言成功
// @Failure 403 删除二级留言留言失败
// @router /delLeaveWordTwoById/:id [delete]
func (c *LeaveWordController) DeleteLeaveWordTwoById() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Delete(&models.LeaveWordTwo{Id: id})
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "删除失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("删除成功")
	c.ServeJSON()
}

// UpdateLeaveWordPass
// @Title	UpdateLeaveWordPass
// @Description 一级留言审核通过
// @Param id path	int	   true	"一级留言的id"
// @Success 201 修改一级留言留言成功
// @Failure 403 修改一级留言留言失败
// @router /updateLeaveWordPass/:id [put]
func (c *LeaveWordController) UpdateLeaveWordPass() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Update(&models.LeaveWord{Id: id, Pass: 1}, "Pass")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "修改失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("修改成功")
	c.ServeJSON()
}

// UpdateLeaveWordTwoPass
// @Title	UpdateLeaveWordTwoPass
// @Description 二级留言审核通过
// @Param id path	int	   true	"二级留言的id"
// @Success 201 修改二级留言留言成功
// @Failure 403 修改二级留言留言失败
// @router /updateLeaveWordTwoPass/:id [put]
func (c *LeaveWordController) UpdateLeaveWordTwoPass() {
	id, err := c.GetInt64(":id")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "id格式错误")
		c.ServeJSON()
		return
	}
	_, err = c.O.Update(&models.LeaveWordTwo{Id: id, Pass: 1}, "Pass")
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "修改失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk("修改成功")
	c.ServeJSON()
}

// GetAllLeaveWordNotPass
// @Title	GetAllLeaveWordNotPass
// @Description 查询所有未通过的留言
// @Success 201 获取所有未通过的留言成功
// @Failure 403 获取所有未通过的留言失败
// @router /getAllLeaveWordNotPass/ [get]
func (c *LeaveWordController) GetAllLeaveWordNotPass() {
	var leavewords []models.LeaveWord
	_, err := c.O.QueryTable(new(models.LeaveWord).TableName()).Filter("Pass", 0).OrderBy("-id").All(&leavewords)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "查询所有留言失败")
		c.ServeJSON()
		return
	}
	var leavewordtwos []models.LeaveWordTwo
	_, err = c.O.QueryTable(new(models.LeaveWordTwo).TableName()).Filter("Pass", 0).OrderBy("-id").All(&leavewordtwos)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "查询所有留言失败")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"leaveWord":     leavewords,
		"leavewordtwos": leavewordtwos,
	})
	c.ServeJSON()
}

// GetLeaveWordByPage
// @Title GetLeaveWordByPage
// @Description 分页获取未通过的一级留言
// @Param pagesize	path	int true	"pagesize"
// @Param page	path	int true	"page"
// @Success 201 获取成功
// @Failure 403 获取失败
// @router /getLeaveWordByPage/ [get]
func (c *LeaveWordController) GetLeaveWordByPage() {
	qs := c.O.QueryTable(new(models.LeaveWord).TableName()).Filter("Pass", 0).OrderBy("-id")
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var leaveWords []models.LeaveWord
	_, err = qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&leaveWords)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":        count,
		"pageNum":    pageNum,
		"pageCount":  pageCount,
		"leaveWords": leaveWords,
	})
	c.ServeJSON()
}

// GetLeaveWordTwoByPage
// @Title GetLeaveWordTwoByPage
// @Description 分页获取未通过的二级留言
// @Param pagesize	path	int true	"pagesize"
// @Param page	path	int true	"page"
// @Success 201 获取成功
// @Failure 403 获取失败
// @router /getLeaveWordTwoByPage/ [get]
func (c *LeaveWordController) GetLeaveWordTwoByPage() {
	qs := c.O.QueryTable(new(models.LeaveWordTwo).TableName()).Filter("Pass", 0).OrderBy("-id")
	count, err := qs.Count()
	pageSetNum, err := c.GetInt("pagesize")
	if err != nil {
		pageSetNum = 1
	}
	//	总页数
	pageCount := math.Ceil(float64(count) / float64(pageSetNum))
	//	获取当前页码
	pageNum, err := c.GetInt("page")
	if err != nil {
		pageNum = 1
	}
	var leaveWordTwos []models.LeaveWordTwo
	_, err = qs.Limit(pageSetNum, pageSetNum*(pageNum-1)).All(&leaveWordTwos)
	if err != nil {
		c.Data["json"] = models.GetRevaErr(err, "错误")
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.GetRevaOk(map[string]interface{}{
		"num":           count,
		"pageNum":       pageNum,
		"pageCount":     pageCount,
		"leaveWordTwos": leaveWordTwos,
	})
	c.ServeJSON()
}

// GetLeaveWordNumInAWeek
// @Title	GetLeaveWordNumInAWeek
// @Description 获取一周的留言量
// @Success 201 获取一周的留言量成功
// @Failure 403 获取一周的留言量失败
// @router /getLeaveWordNumInAWeek/ [get]
func (c *LeaveWordController) GetLeaveWordNumInAWeek() {
	count1, _ := c.O.QueryTable(new(models.LeaveWord).TableName()).Filter("time_gt", time.Now().Add(-time.Hour*24*7)).Count()
	count2, _ := c.O.QueryTable(new(models.LeaveWordTwo).TableName()).Filter("time_gt", time.Now().Add(-time.Hour*24*7)).Count()
	c.Data["json"] = models.GetRevaOk(count1 + count2)
	c.ServeJSON()
}
